package com.order.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.order.bean.Admin;
import com.order.bean.Order;
import com.order.bean.Tables;
import com.order.common.exception.CommonException;
import com.order.service.TablesService;
import com.order.service.impl.TablesServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "TablesController", value = "/tables/*")
public class TablesController extends BaseController {

    private TablesService tablesService = new TablesServiceImpl();

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json = request.getReader().readLine();
        Tables tables = JSON.parseObject(json, Tables.class);
        boolean b = tablesService.addTable(tables);
        this.writeResult(response, b);
    }

    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        boolean b = tablesService.deleteTableById(id);
        this.writeResult(response, b);
    }

    protected void deleteBatch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idsStr = request.getReader().readLine();
        Integer[] ids = JSON.parseObject(idsStr, Integer[].class);
        boolean b = tablesService.deleteBatchTables(ids);
        this.writeResult(response, b);
    }


    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json = request.getReader().readLine();
        Tables tables = JSON.parseObject(json, Tables.class);
        boolean b = tablesService.updateTable(tables);
        this.writeResult(response, b);
    }

    protected void addOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json = request.getReader().readLine();
        Tables tables = JSON.parseObject(json, Tables.class);
        Order order = tablesService.addOrder(tables);
        this.writeResultAndData(response, order);
    }

    protected void removeOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json = request.getReader().readLine();
        Tables tables = JSON.parseObject(json, Tables.class);
        tablesService.removeOreder(tables);
        this.writeResultAndData(response, true);
    }

    protected void selectById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        Tables tables = tablesService.selectTableById(id);
        this.writeResultAndData(response, tables);
    }

    protected void selectByUserId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        Tables tables = tablesService.selectTableById(userId);
        this.writeResultAndData(response, tables);
    }

    protected void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        List<Tables> tables = tablesService.selectAllTables(name);
        this.writeResultAndData(response, tables);
    }

    protected void selectPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        Integer pageNum = Integer.valueOf(request.getParameter("pageNum"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        PageInfo<Tables> tablesPageInfo = tablesService.selectTablesByPage(name, pageNum, pageSize);
        this.writeResultAndData(response, tablesPageInfo);
    }



}
